package com.baikeyang.service.activity.impl;

import cn.hutool.core.util.StrUtil;
import com.baikeyang.common.bean.ResponseResult;
import com.baikeyang.common.bean.activity.SimpleActivityInfo;
import com.baikeyang.common.bean.activity.SimpleActivityUser;
import com.baikeyang.common.constant.ExecuteConstants;
import com.baikeyang.common.model.*;
import com.baikeyang.common.util.shiro.ShiroUtil;
import com.baikeyang.mapper.ActivityInfoMapper;
import com.baikeyang.service.WxUser.IWxUserService;
import com.baikeyang.service.activity.IActivityInfoService;
import com.baikeyang.service.activity.IActivityUserCareService;
import com.baikeyang.service.activity.IActivityUserFavService;
import com.baikeyang.service.activity.IActivityUserJoinService;
import com.baikeyang.service.base.impl.SuperServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author BaiKeyang
 * @since 2020-05-20
 */
@Service
public class IActivityInfoServiceImpl extends SuperServiceImpl<ActivityInfoMapper, ActivityInfo> implements IActivityInfoService {

    @Autowired
    private ActivityInfoMapper activityInfoMapper;
    @Autowired
    private IWxUserService wxUserService;
    @Autowired
    private IActivityUserJoinService activityUserJoinService;
    @Autowired
    private IActivityUserFavService activityUserFavService;
    @Autowired
    private IActivityUserCareService activityUserCareService;


    @Override
    public ResponseResult saveActivityInfo(ActivityInfo activityInfo) {
        try{
            activityInfo.setCreateTime(new Date());
            activityInfo.setCreateUser(ShiroUtil.getCurrentUserId());
            activityInfo.setUpdateTime(new Date());
            activityInfo.setUpdateUser(ShiroUtil.getCurrentUserId());
            activityInfo.setReleaseTime(new Date());
            activityInfo.setReleaseUser(ShiroUtil.getCurrentUserId());
            int i = activityInfoMapper.insert(activityInfo);
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
        }catch (Exception e) {
            return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
        }
    }

    @Override
    public ResponseResult updateActivityInfo(ActivityInfo activityInfo) {
        try{
            activityInfo.setCreateTime(new Date());
            activityInfo.setCreateUser(ShiroUtil.getCurrentUserId());
            activityInfo.setUpdateTime(new Date());
            activityInfo.setUpdateUser(ShiroUtil.getCurrentUserId());
            activityInfo.setReleaseTime(new Date());
            activityInfo.setReleaseUser(ShiroUtil.getCurrentUserId());
            int i = 0;
            if(null != activityInfo.getId()){
                i = activityInfoMapper.updateById(activityInfo);
            } else {
                i = activityInfoMapper.insert(activityInfo);
            }
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
        }catch (Exception e) {
            return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
        }
    }

    @Override
    public IPage<SimpleActivityInfo> getSimpleActivityInfoPage(Page<ActivityInfo> pageParam) {
        IPage<SimpleActivityInfo> activityInfoIPage = activityInfoMapper.selectSimpleActivityInfos(pageParam);
        return activityInfoIPage;
    }

    @Override
    public ResponseResult getActivityInfoById(String id) {
        ActivityInfo activityInfo = new ActivityInfo();
        if(StrUtil.isNotEmpty(id)){
            activityInfo = activityInfoMapper.selectById(id);
        }
        return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, activityInfo);
    }

    @Override
    public ResponseResult joinActivityByUserId(ActivityUserJoin activityUserJoin) {
        if(StrUtil.isNotEmpty(activityUserJoin.getOpenId())){
            WxUser wxUser = wxUserService.getWxUserByOpenId(activityUserJoin.getOpenId());
            if(null == wxUser) {
                return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
            }
            activityUserJoin.setUserId(wxUser.getId());
            int i = activityUserJoinService.saveActivityUserJoin(activityUserJoin);
            if(i > 0){
                return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
            }
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public ResponseResult favActivityByUserId(ActivityUserFav activityUserFav) {
        int i = activityUserFavService.saveActivityUserFav(activityUserFav);
        if(i > 0){
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public ResponseResult careActivityByUserId(ActivityUserCare activityUserCare) {
        int i = activityUserCareService.saveActivityUserCare(activityUserCare);
        if(i > 0){
            return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200);
        }
        return new ResponseResult(ExecuteConstants.EXECUTEINFO.失败, ExecuteConstants.EXECUTECODE.CODE_500);
    }

    @Override
    public IPage<SimpleActivityInfo> getActivityUserJoins(Page<SimpleActivityInfo> pageParam, Long userId) {
        IPage<SimpleActivityInfo> activityInfoIPage = activityInfoMapper.selectJoinSimpleActivityInfos(pageParam, userId);
        return activityInfoIPage;
    }

    @Override
    public IPage<SimpleActivityInfo> getActivityUserFavs(Page<SimpleActivityInfo> pageParam, Long userId) {
        IPage<SimpleActivityInfo> activityInfoIPage = activityInfoMapper.selectFavSimpleActivityInfos(pageParam, userId);
        return activityInfoIPage;
    }

    @Override
    public IPage<SimpleActivityUser> getActivityUserCares(Page<SimpleActivityUser> pageParam, Long userId) {
        IPage<SimpleActivityUser> activityInfoIPage = wxUserService.selectActivityUserCares(pageParam, userId);
        return activityInfoIPage;
    }

    @Override
    public ResponseResult checkActivityUserJoin(Long userId, Long activityId) {
        int i = activityUserJoinService.getActivityUserJoinByUserIdAndActivityId(userId, activityId);
        boolean flag = false;
        if(i > 0){
            flag = true;
        }
        return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, flag);
    }

    @Override
    public ResponseResult checkActivityUserFav(Long userId, Long activityId) {
        int i = activityUserFavService.getActivityUserFavByUserIdAndActivityId(userId, activityId);
        boolean flag = false;
        if(i > 0){
            flag = true;
        }
        return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, flag);
    }

    @Override
    public ResponseResult checkActivityUserCare(Long userId, Long careUserId) {
        int i = activityUserCareService.getActivityUserCareByUserIdAndCareUserId(userId, careUserId);
        boolean flag = false;
        if(i > 0){
            flag = true;
        }
        return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, flag);
    }

    @Override
    public ResponseResult removeJoinActivityByUserId(String userId, Long activityId) {
        int i = activityUserJoinService.delActivityUserJoinByUserIdAndActivityId(userId, activityId);
        boolean flag = false;
        if(i > 0){
            // 满员限制
            // 对参加活动者的资格审核
            // 判断是否可以取消
            // TODO 退款
            flag = true;
        }
        return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, flag);
    }

    @Override
    public ResponseResult removeFavActivityByUserId(String userId, Long activityId) {
        int i = activityUserFavService.delActivityUserFavByUserIdAndActivityId(userId, activityId);
        boolean flag = false;
        if(i > 0){
            flag = true;
        }
        return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, flag);
    }

    @Override
    public ResponseResult removeCareActivityByUserId(String userId, Long careUserId) {
        int i = activityUserCareService.delActivityUserCareByUserIdAndCareUserId(userId, careUserId);
        boolean flag = false;
        if(i > 0){
            flag = true;
        }
        return new ResponseResult(ExecuteConstants.EXECUTESTATUS.成功, ExecuteConstants.EXECUTEINFO.成功, ExecuteConstants.EXECUTECODE.CODE_200, flag);
    }
}
